//
//  JourneyViewController.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-3-21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "JourneyViewController.h"
#import "JourneyHeaderView.h"
#import "JourneyCollectionViewCell.h"
#import "JourneryNetHandler.h"
#import "JourneyDataSource.h"
#import "JourneyDetilDataSource.h"
@interface JourneyViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic ,retain)JourneyDataSource *journeyData;
@property (nonatomic , retain)UICollectionView *collectionView;
@property (nonatomic , retain)NSMutableArray *journeyArr;
@property (nonatomic , retain)NSMutableArray *journeyDetilArr;
@property (nonatomic , retain)NSMutableArray *journeyARR;
@end

@implementation JourneyViewController
-(void)dealloc
{
    [_collectionView release];
    [_journeyArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.journeyData = [[JourneyDataSource alloc]init];
    [self netHandler];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(170, 150);
    //最小列间距
    flowLayout.minimumInteritemSpacing = 1;
    //最小行间距
    flowLayout.minimumLineSpacing = 10;
    //边缘距离
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 10, 20, 15);
    //设置头部并给定大小
    [flowLayout setHeaderReferenceSize:CGSizeMake(_collectionView.frame.size.width, 50)];
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;

#pragma mark 注册collecttionView
    //注册collecttionView
    [_collectionView registerClass:[JourneyCollectionViewCell class] forCellWithReuseIdentifier:@"旅行cell"];

#pragma mark 注册头部视图
    //注册头部视图
    [_collectionView registerClass:[JourneyHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    
    //添加视图
    [self.view addSubview:_collectionView];
    
    
    //内存管理
    [_collectionView release];
    [flowLayout release];
}

//collectionView的section的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return [self.journeyArr count];
}

//collectionView 每个section cell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//    NSLog(@"section = %ld",section);
    JourneyDataSource *jdsModel = [self.journeyArr objectAtIndex:section];
    
    
    
    return [jdsModel.data count];
}


#pragma mark 设置头部视图

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"头部视图");
    UICollectionReusableView *reusableView = nil;
    //if (kind == UICollectionElementKindSectionHeader) {
        //定制头部视图的内容
        JourneyHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
    
    JourneyDataSource *jdsModel = [_journeyArr objectAtIndex:indexPath.section];
    
        headerView.headerLable.text = jdsModel.title;
 
        reusableView = headerView;
    //}
    
    return  reusableView;
}
#pragma mark cell 赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
        JourneyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"旅行cell" forIndexPath:indexPath];
    JourneyDataSource * jdsModel = _journeyArr[indexPath.section];
    JourneyDetilDataSource * data = jdsModel.data[indexPath.row];
    cell.jourDetilData = data;
    NSLog(@"indexPath = %@, data.cover_s = %@",indexPath,data.cover_s);
        return cell;
    
  
}
#pragma mark CELL 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"section = %ld",indexPath.section);
    NSLog(@"item = %ld", indexPath.item);
}

#pragma mark 数据解析
- (void)netHandler
{
    NSString *str = @"http://api.breadtrip.com/destination/v3/";
    self.journeyArr = [NSMutableArray array];
//    self.journeyDetilArr = [NSMutableArray array];
    [JourneryNetHandler getDataWithUrl:str completion:^(NSData *data) {

        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSMutableArray *Arr = [resultDic objectForKey:@"elements"];
//        self.journeyData.data = [[NSMutableArray alloc]init];
        for (NSMutableDictionary *dic in Arr) {
//            [self.journeyArr addObject: _journeyData];
            
            JourneyDataSource *jdsModel = [[JourneyDataSource alloc] initWithDictionary:dic];
            
             NSMutableArray * dataArray = [dic objectForKey:@"data"];
//            [_journeyArr addObject:[dic objectForKey:@"title"]];
            
            NSMutableArray *secArr = [NSMutableArray array];
            
            for (NSMutableDictionary *dic in dataArray) {
                JourneyDetilDataSource *data = [[JourneyDetilDataSource alloc]initWithDictionary:dic];
                [secArr addObject:data];
//                NSLog(@"%@",secArr);
                [data release];
            }
            
            jdsModel.data = secArr;
            
            [self.journeyArr addObject:jdsModel];
            [jdsModel release];
//            [_journeyData.data addObject:secArr];
        }
        
        [_collectionView reloadData];
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
